#include "class_strategy_trend.h"
#include "class_center.h"
#include "class_strategy_position.h"

extern class_center a;
extern lambda_pool pool;


class_strategy_trend::class_strategy_trend(int idd, std::string dm, int n1, int sl)
{
memset(&ints,0,sizeof(ints));
memset(&dous,0,sizeof(dous));
memset(&data,0,sizeof(data));

name = dm;

std::string str = a.path_str + "/strategy";
create_path(str);

str = a.path_str + "/strategy/trend";
create_path(str);

str = a.path_str + "/strategy/trend/"+dm;
create_path(str);

str = a.path_str + "/log/strategy";
create_path(str);

str = a.path_str + "/log/strategy/trend";
create_path(str);

ints[1] = n1;
ints[2] = sl;

cc = new class_strategy_position(a.path_str + "/strategy/trend/" + dm);

bars.read(a.path_str+"/bars/" + name + ".dat", n1+2);
if(a.bars_map.find(dm) != a.bars_map.end() )
{
for(auto &v: a.bars_map[dm].datas)
{
bars.datas.push_back(v);
}
}

//周期高低点
dous[1] = bars.highest(ints[1] * 2, ints[1] * 3);
dous[2] = bars.lowest(ints[1] * 2, ints[1] * 3);
dous[3] = bars.highest(ints[1], ints[1] * 2);
dous[4] = bars.lowest(ints[1], ints[1] * 2);
dous[5] = bars.highest(0, ints[1]);
dous[6] = bars.lowest(0, ints[1]);
id=idd;
a.tick_event_map[name].insert([this](){ pool.insert([this](){ if(a.flag[50]==1){run_one();} }); },id);
a.log.insert("strategy/trend/"+name,"实例化成功,当前仓位" + std::to_string(cc->position)
+	"周期1高=" + std::to_string(dous[1])
+	"周期2高=" + std::to_string(dous[3])
+	"周期3高=" + std::to_string(dous[5])
+	"周期1低=" + std::to_string(dous[2])
+	"周期2低=" + std::to_string(dous[4])
+	"周期3低=" + std::to_string(dous[6])
);
}

class_strategy_trend::~class_strategy_trend()
{
a.tick_event_map[name].pop(id);
	a.log.insert("strategy/trend/"+name,"析构完成,当前仓位"+std::to_string(cc->position));
delete cc;
}


// 主执行函数
void class_strategy_trend::run_one()
{


//清理报单返回值
a.return_erase(*cc);
//同步持仓
a.position_sync(*cc);
//获取到TICK
data=a.tick_map[name];
//获取行情到bar
bars.insert(data);
if (bars.datas.size() < (ints[1] * 3 + 2) || a.flag[35] == 0)
{
	std::cout<<"趋势"<<name<<"bar数量"<<bars.datas.size()<<std::endl;
return;
}




if (bars.datas.size() != ints[9])
{
ints[9] = bars.datas.size();
//周期高低点
dous[1] = bars.highest(ints[1] * 2, ints[1] * 3);
dous[2] = bars.lowest(ints[1] * 2, ints[1] * 3);
dous[3] = bars.highest(ints[1], ints[1] * 2);
dous[4] = bars.lowest(ints[1], ints[1] * 2);
dous[5] = bars.highest(0, ints[1]);
dous[6] = bars.lowest(0, ints[1]);
a.log.insert("strategy/trend/" + name,
	"周期1高=" + std::to_string(dous[1])
+	"周期2高=" + std::to_string(dous[3])
+	"周期3高=" + std::to_string(dous[5])
+	"周期1低=" + std::to_string(dous[2])
+	"周期2低=" + std::to_string(dous[4])
+	"周期3低=" + std::to_string(dous[6])
	+ "持仓" + std::to_string(cc->position));
}


//下面是数据计算
if(dous[5] > dous[3] && dous[3] > dous[1] && dous[6] > dous[4] && dous[4] > dous[2])
{
ints[3] = ints[2];
}
else if(dous[5] < dous[3] && dous[3] < dous[1] && dous[6] < dous[4] && dous[4] < dous[2])
{
ints[3] = ints[2] * -1;
}
else
{
ints[3] = 0;
}

//下面是报单
//如果处于买入区且仓位不够则
if (cc->position < ints[3] && cc->return_vector.size() == 0)
{
a.bd_auto(data.InstrumentID, '0', data.UpperLimitPrice, 1, *cc);
a.log.insert("strategy/trend/" + name +"交易", name + "多动作" + std::to_string(data.AskPrice1)
+	"周期1高=" + std::to_string(dous[1])
+	"周期2高=" + std::to_string(dous[3])
+	"周期3高=" + std::to_string(dous[5])
+	"周期1低=" + std::to_string(dous[2])
+	"周期2低=" + std::to_string(dous[4])
+	"周期3低=" + std::to_string(dous[6])
);
return;
}


if (cc->position > ints[3] && cc->return_vector.size() == 0)
{
a.bd_auto(data.InstrumentID, '1', data.LowerLimitPrice, 1,*cc);
a.log.insert("strategy/trend/" + name + "交易", name + "空动作" + std::to_string(data.BidPrice1)
+	"周期1高=" + std::to_string(dous[1])
+	"周期2高=" + std::to_string(dous[3])
+	"周期3高=" + std::to_string(dous[5])
+	"周期1低=" + std::to_string(dous[2])
+	"周期2低=" + std::to_string(dous[4])
+	"周期3低=" + std::to_string(dous[6])
);
return;
}

return;
}

